package com.website.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.website.common.controller.BaseController;
import com.website.common.warpper.ResultMap;
import com.website.system.model.DrugsType;
import com.website.system.model.Yaopin;
import com.website.system.model.vo.TypeVO;
import com.website.system.service.DrugsService;
import com.website.system.service.DrugsTypeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("manage/Drugs")
public class DrugsController extends BaseController {
    @Resource
    private DrugsService yaopinservice;
    @Resource
    private DrugsTypeService drugsTypeService;
    //跳转到Drugs页面
    @RequestMapping("")
    public String drugs() {
        return "manage/Drugs";
    }
    /**
     * 加载Drugs数据信息
     */
    @ResponseBody
    @RequestMapping("/DrugsTable")
    public ResultMap drugsTable(@RequestParam Integer page, @RequestParam Integer limit,  @RequestParam(required = false) String name) {
        ResultMap<List<TypeVO>> resultMap = new ResultMap<>(SUCCESS_CODE, SUCCESS_MSG, null, null);
        Page<Yaopin> pageInfo = new Page<>(page, limit);
        Wrapper<Yaopin> wrapper = new EntityWrapper<>();
        wrapper = name == null ? wrapper : wrapper.like("name", name);
        List<Yaopin> drugs = yaopinservice.selectPage(pageInfo,wrapper).getRecords();
        resultMap.setCount(yaopinservice.selectCount(wrapper));
        List<TypeVO> typeVOList= new ArrayList<>();
        for (Yaopin yaopin:drugs){
            TypeVO typeVO=new TypeVO();
            DrugsType drugsType=drugsTypeService.selectById(yaopin.getTypeId());
            typeVO.setId(yaopin.getId());
            typeVO.setName(yaopin.getName());
            typeVO.setAction(yaopin.getAction());
            typeVO.setBuytime(yaopin.getBuytime());
            typeVO.setSelltime(yaopin.getSelltime());
            typeVO.setStock(yaopin.getStock());
            typeVO.setTypeId(yaopin.getTypeId());
            typeVO.setTypeName(drugsType.getTypeName());
            typeVOList.add(typeVO);
        }
        resultMap.setData(typeVOList);
        return resultMap;
    }
//删除
    @ResponseBody
    @RequestMapping(value = "/deleteDrugs", method = RequestMethod.POST)
    public String deleteYaopin(@Param("id") Integer id) {
        try {
            String name = yaopinservice.selectById(id).getName();
            deleteFile(name);
            yaopinservice.deleteById(id);
            return SUCCESS_MSG;
        } catch (Exception e) {
            return ERROR_MSG;
        }
    }
    /*
     * 查看
     * */
    @RequestMapping("/toReadDrugs/{type}")
    public String YaopinRead(Model model, @PathVariable Integer type ) {
        Yaopin yaopin=yaopinservice.selectById(type);
        model.addAttribute("Drugs", yaopin);
        DrugsType drugsType=drugsTypeService.selectById(yaopin.getTypeId());
        model.addAttribute("drugsType", drugsType);
        return "manage/readDrugs";
    }
    /**
     * 修改页面信息
     */
    @RequestMapping("/toUpdateDrugs/{type}")
    public String getAssociation(Model model, @PathVariable Integer type) {
        model.addAttribute("Drugs", yaopinservice.selectById(type));
        return "manage/updateDrugs";
    }
    /**
     * 保存修改信息
     */
    @ResponseBody
    @RequestMapping("/updateDrugs")
    public String yaopinUpdate(Yaopin yaopin) {
        try {
            Yaopin yaoPins=yaopinservice.selectById(yaopin.getId());
            yaoPins.setName(yaopin.getName());
            yaoPins.setAction(yaopin.getAction());
            yaoPins.setTypeId(yaopin.getTypeId());
            yaopinservice.updateById(yaoPins);
            return SUCCESS_MSG;
        } catch (Exception e) {
            return ERROR_MSG;
        }
    }
    /**
     * 跳转到添加
     */
    @RequestMapping("/toAddYaopin")
    public String yaopinadd() {
        return "manage/addDrugs";
    }
   /**
     * 保存添加
     */
    @ResponseBody
    @RequestMapping("/addYaopin")
    public String addRolesOne(Yaopin yaopin) {
        yaopinservice.insert(yaopin);
        return "添加成功";
    }
}

